<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>千锋教育</title>
    <style>
        #app {
            width: 600px;
            background-color: gray;
        }

        .son {
            width: 350px;
            border: 1px solid green;
            background-color: firebrick;
        }
    </style>
</head>

<body>
    <div id="app">
        <son-com v-for="(item,index) in product" :title="item.title" :price="item.price" :index="index" :key="index"
            @myclick="fuMethod"></son-com>

    </div>
</body>
<script src="./vue.js"></script>
<script>
    /* 
       数据是来自父组件， 点击砍价时，在修改父组件的数据
    */
    Vue.component("sonCom", {
        props: [],
        template: `<div class='son'>
             <h3>标题：{{title}}</h3>
             <p>价格:{{price}}元</p>
             <p>开业大酬宾,全场八折</p>
             <button @click='kanDao'>点我砍价</button>
        </div>`,
        props: ["title", "price", 'index'],
        data() {
            return {}
        },
        methods: {
            kanDao() {
                // 避免直接更改父组件的属性，因为每当父组件重新渲染时，该值将被覆盖
                // this.price--;  不合适
                // 遵循的原则是谁的数据谁来改
                // console.log(this.index); // 子传父，传递是下标   ，告知父元素我点击的是你的哪一项

                this.$emit("myclick", this.index, 3)

            }
        }
    })

    const vm = new Vue({
        el: '#app',
        data: {
            product: [
                { title: "超级好吃的口水鸡", price: 50 },
                { title: "超级好吃的榴莲", price: 100 },
                { title: "超级好吃的土豆", price: 10 },
            ]
        },
        methods: {
            fuMethod(index, val) {
                console.log(index);

                this.product[index].price -= val;
            }
        }
    })
</script>

</html>